package com.boot.unity.api.modules.rzgjj.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.common.enums.DataSourceType;
import com.boot.unity.api.config.DataSource;
import com.boot.unity.api.config.DynamicDataSourceContextHolder;
import com.boot.unity.api.config.StringUtils;
import com.boot.unity.api.modules.dgnsh.dao.GjjyqlxDao;
import com.boot.unity.api.modules.rzgjj.dao.GjjDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GjjService {

    @Autowired
    private GjjDao gjjDao;

    @Autowired
    private GjjyqlxDao gjjyqlxDao;

    private static String changeType(String typeCode){
        if(StringUtils.isEmpty(typeCode)){
            typeCode = "";
        }
        if(typeCode.trim().equals("1001")){
            return "01";
        }
        return typeCode;
    }


    @DataSource(value = DataSourceType.SLAVE)
    public List<JSONObject> getAllData(){
        //手动切换数据源
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        List<JSONObject> list = gjjyqlxDao.getAllData();
        DynamicDataSourceContextHolder.clearDataSourceType();
        return list;
    }


    public static void main(String[] args) {
        JSONObject nObj = new JSONObject();
        nObj.put("CODENO","1XY");
        nObj.put("CODETYPE","1001");
        nObj.put("rownum","5");
        nObj.put("mntindx","0019");
        /*System.out.println(transToLowerObject(json.toString()));
        System.out.println(StringUtils.isEmpty(""));*/


        /*nObj = transToLowerObject(nObj.toString());
        if(!StringUtils.isEmpty(nObj.getString("codetype"))){
            String codetype = nObj.getString("codetype");
            nObj.remove("codetype");
            nObj.put("codetype",changeType(codetype));
        }
        nObj.remove("rownum");
//        datalist.add(nObj);

        System.out.println(nObj.toString());*/
//        System.out.println(nObj.getInteger("mntindx"));
    }


    public static JSONObject transToLowerObject(String json) {
        JSONObject JSONObject2 = new JSONObject();
        JSONObject JSONObject1 = JSON.parseObject(json);
        for (String key : JSONObject1.keySet()){
            Object object = JSONObject1.get(key);
            if (object.getClass().toString().endsWith("JSONObject")) {
                JSONObject2.put(key.toLowerCase(), transToLowerObject(object.toString()));
            } else if (object.getClass().toString().endsWith("JSONArray")) {
                JSONObject2.put(key.toLowerCase(), transToArray(JSONObject1.getJSONArray(key).toString()));
            }else{
                JSONObject2.put(key.toLowerCase(), object);
            }
        }
        return JSONObject2;
    }


    public static JSONArray transToArray(String jsonArray) {
        JSONArray jSONArray2 = new JSONArray();
        JSONArray jSONArray1 = JSON.parseArray(jsonArray);
        for (int i = 0; i < jSONArray1.size(); i++) {
            Object jArray = jSONArray1.getJSONObject(i);
            if (jArray.getClass().toString().endsWith("JSONObject")) {
                jSONArray2.add(transToLowerObject( jArray.toString()));
            } else if (jArray.getClass().toString().endsWith("JSONArray")) {
                jSONArray2.add(transToArray(jArray.toString()));
            }
        }
        return jSONArray2;
    }


    //职工银行卡有效性校验
    @DataSource(value = DataSourceType.MASTER)
    public JSONObject queryBAS001(String pername,String codetype,String codeno,String bankcode,String accounttype,int pageSize,int currentPage){
        int endNum = pageSize*currentPage;
        int startNum = (currentPage-1)*pageSize;
        int valNum = gjjDao.queryBAS001Count(pername, codetype, codeno, bankcode, accounttype);
        List<JSONObject> data = gjjDao.queryBAS001Data(pername, codetype, codeno, bankcode, accounttype, startNum, endNum);
        List<JSONObject> datalist = new ArrayList<>();
        for(JSONObject nObj:data){
            nObj = transToLowerObject(nObj.toString());
            if(!StringUtils.isEmpty(nObj.getString("codetype"))){ // 进行更新匹配代码类型
                codetype = nObj.getString("codetype");
                nObj.remove("codetype");
                nObj.put("codetype",changeType(codetype));
            }
            nObj.remove("rownum");
            datalist.add(nObj);
        }
        JSONObject json = new JSONObject();
        json.put("total",valNum);
        json.put("rows",datalist);
        return json;
    }

    // 商业银行贷款合同信息
    @DataSource(value = DataSourceType.MASTER)
    public JSONObject queryBLS001(String pername,String codetype,String codeno,int pageSize,int currentPage){
        JSONObject json = new JSONObject();
        int endNum = pageSize*currentPage;
        int startNum = (currentPage-1)*pageSize;

        int total = gjjDao.queryBLS001Count(pername, codetype, codeno);
        if(total == 0){
//            json.put("cardcode",codeno);
//            json.put("codetype",codetype);
//            json.put("pername",pername);
//            json.put("currentPage",currentPage);
//            json.put("pageSize",pageSize);
//            json.put("string1","--");
//            json.put("string2","--");
//            json.put("string3","--");
            json.put("total","0");
            json.put("rows","");
            json.put("rescode","01");
            json.put("retmsg","查询正常");
            return json;
        }
        List<JSONObject> data = gjjDao.queryBLS001Data(pername, codetype, codeno, startNum, endNum);
        List<JSONObject> datalist = new ArrayList<>();
        for(JSONObject nObj:data){
            nObj = transToLowerObject(nObj.toString());
            nObj.remove("rownum");
            if(!StringUtils.isEmpty(nObj.getString("codetype"))){
                codetype = nObj.getString("codetype");
                nObj.remove("codetype");
                nObj.put("codetype",changeType(codetype));
            }
            if(StringUtils.isEmpty(nObj.getString("repayway"))){
                nObj.put("repayway","01");
            }
            if(!StringUtils.isEmpty(nObj.getString("repayway"))){
                String repayway = nObj.getString("repayway");
                nObj.remove("repayway");
                nObj.put("repayway",changeRepayway(repayway));
            }
            datalist.add(nObj);
        }
        json.put("total",total);
        json.put("rescode","01");
        json.put("retmsg","查询正常");
        json.put("rows",datalist);
        return json;
    }



    @DataSource(value = DataSourceType.MASTER)
    public JSONObject queryBLS002ForY(String cardcode, String pername, String agrcode, String begindate, String enddate, int pageSize, int currentPage,String codetype) {
//        int endNum = pageSize * currentPage;
//        int startNum = (currentPage - 1) * pageSize;
//        int valNum = gjjDao.queryBAS002Count(cardcode, pername, agrcode, begindate, enddate);
        //进行查看当前合同编号是否存在，不存在，给返回空数据
        JSONObject json = new JSONObject();
        List<JSONObject> listData = gjjDao.queryHtData(agrcode);
        if(listData.size() == 0){
            json.put("total", 0);
            json.put("rows", "");
            json.put("rescode", "01");
            json.put("retmsg", "查询正常");
            return json;
        }

        List<JSONObject> data = gjjDao.queryBAS002Data_New(cardcode, pername, agrcode, begindate, enddate);
        List<JSONObject> datalist = new ArrayList<>();
        for(JSONObject nObj:data){
            nObj = transToLowerObject(nObj.toString());
            if(!StringUtils.isEmpty(nObj.getString("codetype"))){
                codetype = nObj.getString("codetype");
                nObj.remove("codetype");
                nObj.put("codetype",changeType(codetype));
            }
            nObj.remove("rownum");
            datalist.add(nObj);
        }
        json.put("total", 1);
        json.put("rows", datalist);
        json.put("rescode", "01");
        json.put("retmsg", "查询正常");
        return json;
    }




    /*@DataSource(value = DataSourceType.MASTER)
    public JSONObject queryBLS002(String cardcode, String pername, String agrcode, String begindate, String enddate, int pageSize, int currentPage,String codetype) {
        int endNum = pageSize * currentPage;
        int startNum = (currentPage - 1) * pageSize;
        int valNum = gjjDao.queryBAS002Count(cardcode, pername, agrcode, begindate, enddate);
        List<JSONObject> data = gjjDao.queryBAS002Data(cardcode, pername, agrcode, begindate, enddate, startNum, endNum);
        List<JSONObject> datalist = new ArrayList<>();
        for(JSONObject nObj:data){
            nObj = transToLowerObject(nObj.toString());
            if(!StringUtils.isEmpty(nObj.getString("codetype"))){
                codetype = nObj.getString("codetype");
                nObj.remove("codetype");
                nObj.put("codetype",changeType(codetype));
            }
            nObj.remove("rownum");
            datalist.add(nObj);
        }
        JSONObject json = new JSONObject();
        json.put("total", valNum);
        json.put("rows", datalist);
        return json;
    }*/

    @DataSource(value = DataSourceType.MASTER)
    public JSONObject queryBLS003(String cardcode, String pername, String agrcode, String begindate, String enddate, int pageSize, int currentPage,String codetype) {
        int endNum = pageSize * currentPage;
        int startNum = (currentPage - 1) * pageSize;
        int valNum = gjjDao.queryBLS003NewCount(agrcode,begindate,enddate);//gjjDao.queryBAS003Count(cardcode, pername, agrcode, begindate, enddate);
        List<JSONObject> data = gjjDao.queryBLS003NewData(agrcode,begindate,enddate,startNum, endNum);//queryBAS003Data(cardcode, pername, agrcode, begindate, enddate, startNum, endNum);
        List<JSONObject> datalist = new ArrayList<>();
        String dkzh = "";
//        List<String> listpaymnt = new ArrayList<>();
//        String ymrq = "";
        JSONArray jsonArrayPaymnt = new JSONArray();
        for(JSONObject nObj:data){
            nObj = transToLowerObject(nObj.toString());
            // System.out.println(nObj.toString());
            if(!StringUtils.isEmpty(nObj.getString("codetype"))){  // 进行codetype类型的转换匹配
                System.out.println("----------------------------------------进行身份证号码编号的切换------------------------------------------");
                codetype = nObj.getString("codetype");
//                System.out.println("codetype:::::"+codetype);
//                System.out.println("changeType(codetype):::::"+changeType(codetype));
                nObj.remove("codetype");
                nObj.put("codetype",changeType(codetype));
            }
            nObj.remove("rownum");  //删除多余字段
            if(!StringUtils.isEmpty(nObj.getString("mntindx"))){// 进行格式化还款期数→整数
                System.out.println("----------------------------------------进行格式化还款期数→整数------------------------------------------");
                int mntindx = nObj.getInteger("mntindx");
                nObj.remove("mntindx");
                nObj.put("mntindx",mntindx);
            }
            String paymnt = nObj.getString("paymnt");
            String bkloanacc = nObj.getString("bkloanacc");

            // System.out.println("jsonArrayPaymnt:::::"+jsonArrayPaymnt.toString());
            if(jsonArrayPaymnt.toString().contains(paymnt)){// dkzh.trim().equals(paymnt)
                nObj.put("ratemny","0.00");
                nObj.put("moremny","0.00");
            }else{
                // dkzh = paymnt;
                // ymrq = bkloanacc;
                // listpaymnt.add(paymnt);
                jsonArrayPaymnt.add(paymnt);
                String lx = gjjDao.queryBLS003Lx(bkloanacc,paymnt);
                String fx = gjjDao.queryBLS003Fx(bkloanacc,paymnt);
                if(StringUtils.isEmpty(lx)){
                    lx = "0.00";
                }
                if(StringUtils.isEmpty(fx)){
                    fx = "0.00";
                    nObj.put("paytype","1");
                }else{
                    nObj.put("paytype","4");
                }
                nObj.put("ratemny",lx);
                nObj.put("moremny",fx);
            }



            /*if(dkzh.equals(paymnt)){
                if(ymrq.trim().equals(bkloanacc)){
                    nObj.put("ratemny","0.00");
                    nObj.put("moremny","0.00");
                    nObj.put("paytype","1");
                }else {
                    dkzh = paymnt;
                    ymrq = bkloanacc;
                    String lx = gjjDao.queryBLS003Lx(bkloanacc,paymnt);
                    if(StringUtils.isEmpty(lx)){
                        lx = "0.00";
                    }
                    String fx = gjjDao.queryBLS003Fx(bkloanacc,paymnt);
                    if(StringUtils.isEmpty(fx)){
                        fx = "0.00";
                        nObj.put("paytype","1");
                    }else{
                        nObj.put("paytype","4");
                    }
                    // 进行处理利息和罚息的问题
                    nObj.put("ratemny",lx);
                    nObj.put("moremny",fx);
                }
            }else{
                dkzh = paymnt;
                ymrq = bkloanacc;
                String lx = gjjDao.queryBLS003Lx(bkloanacc,paymnt);
                if(StringUtils.isEmpty(lx)){
                    lx = "0.00";
                }
                String fx = gjjDao.queryBLS003Fx(bkloanacc,paymnt);
                if(StringUtils.isEmpty(fx)){
                    fx = "0.00";
                    nObj.put("paytype","1");
                }else{
                    nObj.put("paytype","4");
                }
                // 进行处理利息和罚息的问题
                nObj.put("ratemny",lx);
                nObj.put("moremny",fx);
            }*/




            datalist.add(nObj);
        }
        JSONObject json = new JSONObject();
        json.put("total", valNum);
        json.put("rows", datalist);
        json.put("rescode", "01");
        json.put("retmsg", "查询正常");
        return json;
    }


    private static String changeRepayway(String repayway){
        if(StringUtils.isEmpty(repayway.trim())){
            repayway = "01";
        }
        /*if(typeCode.trim().equals("1001")){
            return "01";
        }*/
        return repayway;
    }


}
